package com.parking.carsystem.parkingchargesystem.adapter;

import android.app.Activity;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.parking.carsystem.parkingchargesystem.R;
import com.parking.carsystem.parkingchargesystem.activity.CouponActivity;
import com.parking.carsystem.parkingchargesystem.module.CouponTypeModel;

import java.util.ArrayList;

public class CouponViewpagerAdapter extends PagerAdapter {

    private ArrayList<CouponTypeModel.RowsBean> imageData;
    CouponActivity context;
    OnPageItemClickListener itemClickListener;
    private CouponTypeModel.RowsBean rowsBean;

    public OnPageItemClickListener getItemClickListener() {
        return itemClickListener;
    }

    public void setItemClickListener(OnPageItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    @Override
    public int getCount() {
        return Integer.MAX_VALUE;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    //初始化每个条目要显示的内容
    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        //拿着position位置 % 集合.size
        View view = LinearLayout.inflate(context, R.layout.item_coupon, null);
        RelativeLayout pay_coupon = view.findViewById(R.id.pay_coupon);
        if(imageData != null){
            int newposition = position % imageData.size();
            rowsBean = imageData.get(newposition);
            TextView text_coupon = view.findViewById(R.id.text_coupon);
            TextView paking_name = view.findViewById(R.id.paking_name);
            text_coupon.setText(rowsBean.name);
            paking_name.setText(rowsBean.parkinglotName);
        }
        pay_coupon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(rowsBean != null){
                    itemClickListener.onItemClickLisener(rowsBean.id);
                }
            }
        });
        //获取到条目要显示的内容imageview
//        ImageView imageView = new ImageView(context);
//        imageView.setImageResource(imageData.get(newposition));
        //要把 iv加入到 container 中
        container.addView(view);
        return view;
    }

    //销毁条目
    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        //移除条目
        container.removeView((View) object);
    }

    public void setArrayList(ArrayList<CouponTypeModel.RowsBean> list, Activity mainActivity) {
        this.imageData = list;
        context = (CouponActivity) mainActivity;
    }
    public interface OnPageItemClickListener{
        void onItemClickLisener(int id);
    }
}
